The Steps to Create an EXE in Python

 

Create Executable of Python Script using PyInstaller

 

In this short guide, you’ll see how create an executable of a Python script using PyInstaller.

Here are the full steps to accomplish this goal on Windows.

Steps to Create an Executable using PyInstaller

Step 1: Add Python to Windows Path

To start, you may want to add Python to Windows path.

An easy way to add Python to the path is by downloading a recent version of Python, and then checking the box to ‘Add Python to PATH’ at the beginning of the installation:

 Add Python to PATH

Finish the installation, and you should be good to go.

Step 2: Install the PyInstaller Package

Next, open the Windows “Command Prompt” and then type the following command to install the PyInstaller package:

pip install pyinstaller

Step 3: Save your Python Script

Now save your Python script at your desired location.

For illustration purposes, let’s create a simple Python script that displays ‘Hello World!’ when clicking a button:

import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300)

canvas1.pack()

def hello (): 

    label1 = tk.Label(root, text= 'Hello World!', fg='blue', font=('helvetica', 12, 'bold'))

    canvas1.create_window(150, 200, window=label1)

button1 = tk.Button(text='Click Me', command=hello, bg='brown',fg='white')

canvas1.create_window(150, 150, window=button1)

root.mainloop()

For demonstration purposes, let’s say that the Python script is stored in the following folder:

C:\Users\UserName\Desktop\Test

Where Python script is called ‘hello‘ and the file extension is ‘.py‘

Step 4: Create the Executable using PyInstaller

Now you’ll be able to create the executable of the Python script using PyInstaller.

Simply go to the Command Prompt, and then type:

cd followed by the location where your Python script is stored

Here is the command for our example:

C:\Users\UserName>cd C:\Users\UserName\Desktop\Test

Press Enter (after you typed the location where the Python script is stored on your computer).

Then, refer to the following template to create the executable:

pyinstaller --onefile --windowed pythonScriptName.py

Since for our example, the pythonScriptName is ‘hello‘ (and the file extension is .py), then the command to create the executable is:

 

pyinstaller -–onefile --windowed hello.py OR pyinstaller -–onefile --w hello.py

Press Enter for the last time.

Step 5: Run the Executable

Your executable will be created at the location that you specified.

For our example, it will be under the same folder where the ‘hello’ script was originally stored:

C:\Users\UserName\Desktop\Test

The "build" folder will have many files, but the "dist" folder is where the .exe can be found.

 

You’ll notice that few additional files were created at that location.

To find the executable file, open the dist folder. You’ll then see the executable file:

hello

Double click on the file, and you should be able to launch your program (if you get an error message, you may need to install Visual C++ Redistributable).

In our case, once you click on the ‘hello’ executable, you’ll get a display with a single button.

And if you click on that button, you’ll see the following expression:

Hello World!

You can read more about PyInstaller by visiting the PyInstaller manual.

 

 

References

https://datatofish.com/executable-pyinstaller/

 

 

The remainder of this document explains how to include inside the EXE file the company information and the name of the program such that when Process Explorer from Sysinternals or Task Manager from Microsoft is checking the CPU usage, people will know the purpose and of the company of origin?

From ChatGPT on 2-20-2025

 

Alternative Method: Use a .spec File

Instead of creating a separate version resource file, we can define everything directly inside PyInstaller's .spec file.

1. Create a PyInstaller Spec File

Run this command in the folder where your Python script is:

sh

CopyEdit

pyi-makespec --onefile --windowed your_script.py

This will generate a file named your_script.spec.

2. Edit the .spec File

Open your_script.spec in a text editor (Notepad++, VS Code, etc.), and add the version argument to the exe block.

Modify the exe section to look like this:

python

CopyEdit

exe = EXE(

    pyz,

    a.scripts,

    a.binaries,

    a.zipfiles,

    a.datas,

    name='YourProgram.exe',

    debug=False,

    strip=False,

    upx=True,

    console=False,

    version='version_info.txt',  # Add this line

    icon=None

)

3. Create version_info.txt

In the same directory, create a text file named version_info.txt and paste this content:

ini

CopyEdit

VSVersionInfo(

    ffi=FixedFileInfo(

        filevers=(1, 0, 0, 0),

        prodvers=(1, 0, 0, 0),

        mask=0x3f,

        flags=0x0,

        OS=0x40004,

        fileType=0x1,

        subtype=0x0,

        date=(0, 0)

    ),

    kids=[

        StringFileInfo(

            [

                StringTable(

                    '040904b0',

                    [

                        StringStruct('CompanyName', 'Your Company Name'),

                        StringStruct('FileDescription', 'Your Program Description'),

                        StringStruct('FileVersion', '1.0.0.0'),

                        StringStruct('InternalName', 'YourProgram'),

                        StringStruct('LegalCopyright', '© 2025 Your Company Name'),

                        StringStruct('OriginalFilename', 'YourProgram.exe'),

                        StringStruct('ProductName', 'Your Product Name'),

                        StringStruct('ProductVersion', '1.0.0.0')

                    ]

                )

            ]

        ),

        VarFileInfo([VarStruct('Translation', [1033, 1200])])

    ]

)

4. Build the Executable

Now, run PyInstaller using the .spec file:

sh

CopyEdit

pyinstaller your_script.spec

________________________________________

Why This Works

•   The .spec file directly integrates the version information.

•   No need to manually run pyi-grab_version, which sometimes fails on Windows.

•   Your EXE will now show Company Name, Product Name, and Description when viewed in Task Manager or Process Explorer.

________________________________________

Let me know if you run into any issues! 🚀

4o

You said:

It Worked, with a little bit of work. The .spec file already existed, but if I used that version I received errors. Once I used the version you provided and followed your instructions to the letter, it worked perfectly. Thanks again!

ChatGPT said:

You're very welcome! 🎉 I'm glad you got it working! Sometimes, PyInstaller's default .spec file isn't structured quite right for adding version info, so manually editing it does the trick.

If you ever need more tweaks—like adding an icon, embedding other resources, or handling special packaging cases—just let me know. Happy coding!

 

References

https://chatgpt.com/